fix humminbird icon matching, (#1126)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Sat, 10 Jun 2023 22:00:37 +0000 (16:00 -0600)
committerGitHub <noreply@github.com>
Sat, 10 Jun 2023 22:00:37 +0000 (16:00 -0600)
* fix humminbird icon matching,

eliminating one use of xasprintf.
This was broken in 6029c8267 over a decade ago.

* eliminate unnecessary xasprinf usage.

* eliminate another xasprintf usage.

* eliminate xasprintf usage.

* retire xasprintf, xvasprintf.

defs.h
garmin_txt.cc
humminbird.cc
skytraq.cc
util.cc

diff --git a/defs.h b/defs.h
index b4b988993faf9c012422f96222c905c0bdfe552b..5794729a714261d925ef168438f99957316f1f81 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -33,8 +33,6 @@
 #include <QDateTime>                 // for QDateTime
 #include <QDebug>                    // for QDebug
 #include <QList>                     // for QList, QList<>::const_iterator, QList<>::const_reverse_iterator, QList<>::count, QList<>::reverse_iterator
-#include <QScopedPointer>            // for QScopedPointer
-#include <QScopedPointerPodDeleter>  // for QScopedPointerPodDeleter
 #include <QString>                   // for QString
 #include <QStringView>               // for QStringView
 #include <QTextCodec>                // for QTextCodec
@@ -1019,10 +1017,6 @@ inline int case_ignore_strncmp(const QString& s1, const QString& s2, int n)
   return s1.left(n).compare(s2.left(n), Qt::CaseInsensitive);
 }
 
-[[gnu::format(printf, 2, 3)]] int xasprintf(char** strp, const char* fmt, ...);
-[[gnu::format(printf, 2, 3)]] int xasprintf(QString* strp, const char* fmt, ...);
-[[gnu::format(printf, 2, 3)]] int xasprintf(QScopedPointer<char, QScopedPointerPodDeleter>& strp, const char* fmt, ...);
-[[gnu::format(printf, 2, 0)]] int xvasprintf(char** strp, const char* fmt, va_list ap);
 char* strupper(char* src);
 char* strlower(char* src);
 QDateTime make_datetime(QDate date, QTime time, bool is_localtime, bool force_utc, int utc_offset);
index c78be4a364b0b0aa2dcf54b00fcd666b75cc288a..5fd72a8700387d278578641b8bba8cf79d2d143f 100644 (file)
@@ -81,7 +81,7 @@ static grid_type grid_index;
 static int datum_index;
 static const char* datum_str;
 static int current_line;
-static char* date_time_format = nullptr;
+static QString date_time_format;
 static int precision = 3;
 static time_t utc_offs = 0;
 static gtxt_flags_t gtxt_flags;
@@ -199,7 +199,7 @@ init_date_and_time_format()
   const char* t = get_option_val(opt_time_format, kDefaultTimeFormat);
   QString t1 = convert_human_time_format(t);
 
-  xasprintf(&date_time_format, "%s %s", CSTR(d1), CSTR(t1));
+  date_time_format = QStringLiteral("%1 %2").arg(d1, t1);
 }
 
 static void
@@ -384,7 +384,7 @@ print_date_and_time(const time_t time, const bool time_only)
     } else {
       tm = *localtime(&time);
     }
-    strftime(tbuf, sizeof(tbuf), date_time_format, &tm);
+    strftime(tbuf, sizeof(tbuf), CSTR(date_time_format), &tm);
     *fout << QString::asprintf("%s ", tbuf);
   }
   *fout << "\t";
@@ -803,7 +803,8 @@ garmin_txt_wr_deinit()
   fout->close();
   delete fout;
   fout = nullptr;
-  xfree(date_time_format);
+  date_time_format.clear();
+  date_time_format.squeeze();
 }
 
 static void
@@ -950,7 +951,7 @@ strftime_to_timespec(const char* s)
 static QDateTime
 parse_date_and_time(const QString& str)
 {
-  QString timespec = strftime_to_timespec(date_time_format);
+  QString timespec = strftime_to_timespec(CSTR(date_time_format));
   return QDateTime::fromString(QString(str).trimmed(), timespec);
 }
 
@@ -1376,7 +1377,8 @@ garmin_txt_rd_deinit()
   fin->close();
   delete fin;
   fin = nullptr;
-  xfree(date_time_format);
+  date_time_format.clear();
+  date_time_format.squeeze();
 }
 
 static void
index cdf807370e2971c00d90d8fadc5423691dcf4ba0..95b4453eda55093a19caf2634b9290f044826ced 100644 (file)
@@ -645,11 +645,7 @@ HumminbirdFormat::humminbird_write_waypoint(const Waypoint* wpt)
     if (hum.icon == 255) {     /* no success, no try to find the item in a more comlex name */
       hum.icon = 0;    /* i.e. "Diamond" as part of "Diamond, Green" or "Green Diamond" */
       for (int i = 0; i < num_icons; i++) {
-        char* match;
-        xasprintf(&match, "*%s*", humminbird_icons[i]);
-        int j = wpt->icon_descr.compare(match, Qt::CaseInsensitive);
-        xfree(match);
-        if (j != 0) {
+        if (wpt->icon_descr.contains(humminbird_icons[i], Qt::CaseInsensitive)) {
           hum.icon = i;
           break;
         }
@@ -670,8 +666,8 @@ HumminbirdFormat::humminbird_write_waypoint(const Waypoint* wpt)
   be_write32(&hum.north, qRound(north));
 
   QString name = (global_opts.synthesize_shortnames)
-                   ? mkshort_from_wpt(wptname_sh, wpt)
-                   : mkshort(wptname_sh, wpt->shortname);
+                 ? mkshort_from_wpt(wptname_sh, wpt)
+                 : mkshort(wptname_sh, wpt->shortname);
   memset(&hum.name, 0, sizeof(hum.name));
   memcpy(&hum.name, CSTR(name), name.length());
 
@@ -873,10 +869,10 @@ HumminbirdFormat::humminbird_write_rtept(const Waypoint* wpt) const
 void
 HumminbirdFormat::humminbird_write_waypoint_wrapper(const Waypoint* wpt)
 {
-  char* key;
   Waypoint* tmpwpt;
 
-  xasprintf(&key, "%s\01%.9f\01%.9f", CSTRc(wpt->shortname), wpt->latitude, wpt->longitude);
+  QString key = QStringLiteral("%1\01%2\01%3").arg(wpt->shortname)
+                .arg(wpt->latitude, 0, 'f', 9).arg(wpt->longitude, 0, 'f', 9);
   if (!(tmpwpt = map[key])) {
     tmpwpt = const_cast<Waypoint*>(wpt);
     map[key] = const_cast<Waypoint*>(wpt);
@@ -887,8 +883,6 @@ HumminbirdFormat::humminbird_write_waypoint_wrapper(const Waypoint* wpt)
     tmpwpt = const_cast<Waypoint*>(wpt);
     tmpwpt->extra_data = p;
   }
-
-  xfree(key);
 }
 
 void
index 239c18bfa8dc16def42363838390c6407ef50586..fe4ebb63a4d364693b41ecda57d29f57d8116cc0 100644 (file)
@@ -479,7 +479,6 @@ SkytraqBase::skytraq_get_log_buffer_status(uint32_t* log_wr_ptr, uint16_t* secto
   *sectors_total = le_readu16(&MSG_LOG_STATUS_OUTPUT.sectors_total);
 
   // unsigned char log_bool, fifo_mode;
-  char* mystatus;
   unsigned int tmax = le_readu32(&MSG_LOG_STATUS_OUTPUT.max_time);
   unsigned int tmin = le_readu32(&MSG_LOG_STATUS_OUTPUT.min_time);
   unsigned int dmax = le_readu32(&MSG_LOG_STATUS_OUTPUT.max_dist);
@@ -488,9 +487,7 @@ SkytraqBase::skytraq_get_log_buffer_status(uint32_t* log_wr_ptr, uint16_t* secto
   unsigned int vmin = le_readu32(&MSG_LOG_STATUS_OUTPUT.min_speed);
   // log_bool = *(MSG_LOG_STATUS_OUTPUT.datalog_enable);
   // fifo_mode = *(MSG_LOG_STATUS_OUTPUT.log_fifo_mode);
-  xasprintf(&mystatus, "#logging: tmin=%u, tmax=%u, dmin=%u, dmax=%u, vmin=%u, vmax=%u\n", tmin, tmax, dmin, dmax, vmin, vmax);
-  db(1, mystatus);
-  xfree(mystatus);
+  db(1, "#logging: tmin=%u, tmax=%u, dmin=%u, dmax=%u, vmin=%u, vmax=%u\n", tmin, tmax, dmin, dmax, vmin, vmax);
 
   return res_OK;
 }
diff --git a/util.cc b/util.cc
index 3495491953d2f86a0744de342ba6a36de7e86bd7..1d2328fd1861ebc1e4ddbf7b5cc147d087f3314e 100644 (file)
--- a/util.cc
+++ b/util.cc
@@ -24,7 +24,6 @@
 #include <cerrno>                       // for errno
 #include <climits>                      // for INT_MAX, INT_MIN
 #include <cmath>                        // for fabs, floor
-#include <cstdarg>                      // for va_list, va_end, va_start, va_copy
 #include <cstdio>                       // for size_t, vsnprintf, FILE, fopen, printf, sprintf, stderr, stdin, stdout
 #include <cstdlib>                      // for abs, calloc, free, malloc, realloc
 #include <cstring>                      // for strlen, strcat, strstr, memcpy, strcmp, strcpy, strdup, strchr, strerror
@@ -35,7 +34,6 @@
 #include <QDateTime>                    // for QDateTime
 #include <QFileInfo>                    // for QFileInfo
 #include <QList>                        // for QList
-#include <QScopedPointer>               // for QScopedPointer
 #include <QString>                      // for QString
 #include <QTextBoundaryFinder>          // for QTextBoundaryFinder, QTextBoundaryFinder::Grapheme
 #include <QTextCodec>                   // for QTextCodec
@@ -185,137 +183,6 @@ ufopen(const QString& fname, const char* mode)
 #endif
 }
 
-/*
- * Allocate a string using a format list with optional arguments
- * Returns -1 on error.
- * If return value is anything else, *strp will be populated with an
- * allocated string containing the formatted buffer.
- *
- * Freeing that is the responsibility of the caller.
- */
-
-int
-xasprintf(char** strp, const char* fmt, ...)
-{
-  va_list args;
-
-  va_start(args, fmt);
-  int res = xvasprintf(strp, fmt, args);
-  va_end(args);
-
-  return res;
-}
-
-int
-xasprintf(QString* strp, const char* fmt, ...)
-{
-  va_list args;
-  va_start(args, fmt);
-  char* cstrp;
-  int res = xvasprintf(&cstrp, fmt, args);
-  *strp = cstrp;
-  xfree(cstrp);
-  va_end(args);
-
-  return res;
-}
-
-int
-xasprintf(QScopedPointer<char, QScopedPointerPodDeleter>& strp, const char* fmt, ...)
-{
-  va_list args;
-
-  va_start(args, fmt);
-  char* cstrp;
-  int res = xvasprintf(&cstrp, fmt, args);
-  strp.reset(cstrp);
-  va_end(args);
-
-  return res;
-}
-
-int
-xvasprintf(char** strp, const char* fmt, va_list ap)
-{
-  /* From http://perfec.to/vsnprintf/pasprintf.c */
-  /* size of first buffer malloc; start small to exercise grow routines */
-# define       FIRSTSIZE       1
-  char* buf = nullptr;
-  char* newbuf;
-  size_t nextsize = 0;
-  int outsize;
-  va_list args;
-
-  int bufsize = 0;
-  for (;;) {
-    if (bufsize == 0) {
-      if ((buf = (char*) xmalloc(FIRSTSIZE)) == nullptr) {
-        *strp = nullptr;
-        return -1;
-      }
-      bufsize = FIRSTSIZE;
-    } else if ((newbuf = (char*) xrealloc(buf, nextsize)) != nullptr) {
-      buf = newbuf;
-      bufsize = nextsize;
-    } else {
-      xfree(buf);
-      *strp = nullptr;
-      return -1;
-    }
-
-    va_copy(args, ap);
-    outsize = vsnprintf(buf, bufsize, fmt, args);
-    va_end(args);
-
-    if (outsize == -1) {
-      /* Clear indication that output was truncated, but no
-       * clear indication of how big buffer needs to be, so
-       * simply double existing buffer size for next time.
-       */
-      nextsize = bufsize * 2;
-
-    } else if (outsize == bufsize) {
-      /* Output was truncated (since at least the \0 could
-       * not fit), but no indication of how big the buffer
-       * needs to be, so just double existing buffer size
-       * for next time.
-       */
-      nextsize = bufsize * 2;
-
-    } else if (outsize > bufsize) {
-      /* Output was truncated, but we were told exactly how
-       * big the buffer needs to be next time. Add two chars
-       * to the returned size. One for the \0, and one to
-       * prevent ambiguity in the next case below.
-       */
-      nextsize = outsize + 2;
-
-    } else if (outsize == bufsize - 1) {
-      /* This is ambiguous. May mean that the output string
-       * exactly fits, but on some systems the output string
-       * may have been truncated. We can't tell.
-       * Just double the buffer size for next time.
-       */
-      nextsize = bufsize * 2;
-
-    } else {
-      /* Output was not truncated */
-      break;
-    }
-  }
-  /* Prevent us from allocating millions of unused bytes. */
-  /* O.K.: I think this is not the final solution. */
-  if (bufsize > outsize + 1) {
-    const unsigned ptrsz = sizeof(buf);
-    if (((bufsize + ptrsz + 1) / ptrsz) > ((outsize + ptrsz + 1) / ptrsz)) {
-      buf = (char*) xrealloc(buf, outsize + 1);
-    }
-
-  }
-  *strp = buf;
-  return outsize;
-}
-
 void
 printposn(const double c, bool is_lat)
 {